package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.StoreGoodsOrder;
import com.xmy.cultivate.entity.StoreGoodsOrderBatch;
import com.xmy.cultivate.mapper.StoreGoodsOrderBatchMapper;
import com.xmy.cultivate.service.IStoreGoodsOrderBatchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.CommonUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 加订表 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-08-01
 */
@Service
public class StoreGoodsOrderBatchServiceImpl extends ServiceImpl<StoreGoodsOrderBatchMapper, StoreGoodsOrderBatch> implements IStoreGoodsOrderBatchService {

    @Autowired
    StoreGoodsOrderBatchMapper storeGoodsOrderBatchMapper;

    @Override
    public IPage<StoreGoodsOrderBatch> getListForSearch(IPage<?> page, @Param(Constants.WRAPPER) Wrapper wrapper, Integer affirmType){
       return storeGoodsOrderBatchMapper.getListForSearch(page,wrapper,affirmType);
    }

    @Override
    public List<Map<String,Object>> exportOrderList(IPage<?> page, @Param(Constants.WRAPPER) Wrapper wrapper, Integer affirmType){
       storeGoodsOrderBatchMapper.getListForSearch(page,wrapper,affirmType);
       List<StoreGoodsOrderBatch> storeGoodsOrderList = (List<StoreGoodsOrderBatch>) page.getRecords();
       return this.getEportOrderList(storeGoodsOrderList);
    }

    public List<Map<String,Object>> getEportOrderList(List<StoreGoodsOrderBatch> storeGoodsOrderList){
        List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
        for (StoreGoodsOrderBatch storeGoodsOrderBatch : storeGoodsOrderList) {
            Map<String,Object> mapItem = new HashMap<String,Object>();
            mapItem.put("schoolName", storeGoodsOrderBatch.getSchoolName());
            mapItem.put("classYearName", CommonUtil.getYearClassNameForId(storeGoodsOrderBatch.getYearClassId()));
            mapItem.put("classTypeName", storeGoodsOrderBatch.getClassTypeName());
            mapItem.put("affirmTime",storeGoodsOrderBatch.getAffirmTime());
            mapItem.put("status",storeGoodsOrderBatch.getStatus());
            String lessonNameFinal = "lessonNum";
            for (StoreGoodsOrder storeGoodsOrder : storeGoodsOrderBatch.getStoreGoodsOrderList()) {
                String lessonName = lessonNameFinal + storeGoodsOrder.getLessonNum();
                Integer goodsCount = storeGoodsOrder.getGoodsCount();
                mapItem.put(lessonName,goodsCount);
            }
            mapList.add(mapItem);
        }
        return mapList;
    }


    @Override
    public IPage<StoreGoodsOrderBatch> getAlreadyOrderGroup(IPage<?> page, @Param(Constants.WRAPPER) Wrapper wrapper,Integer affirmType){
        return storeGoodsOrderBatchMapper.getAlreadyOrderGroup(page,wrapper,affirmType);
    }

    @Override
    public List<StoreGoodsOrder> getAlreadyOrder(@Param(Constants.WRAPPER) Wrapper wrapper,Integer affirmType){
        return storeGoodsOrderBatchMapper.getAlreadyOrder(wrapper,affirmType);
    }

    @Override
    public Integer getMaxLessonNum(@Param(Constants.WRAPPER) Wrapper wrapper,Integer affirmType){
        return storeGoodsOrderBatchMapper.getMaxLessonNum(wrapper,affirmType);
    }
}
